var moviePageTotal; //电影总页数
var comicPageTotal;//动漫总页数
var tvShowPageTotal;//电视总页数
var hotTotPage;//热门总页数

$(function () {
    initContet();
    initPageTotal();
    writeContentList(1,"movie");
    writeContentList(1,"comic");
    writeContentList(1,"tvShow");
    writeContentList(1,"main");

    $("#moviePageList li:eq(1)").addClass("active").siblings().removeClass("active");
    $(".nav.nav-pills li:not(#search)").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
    });
});
  //--------------------------------------这是单击事件开始-------------------------------------------------//
$("#hotUpPage").click(function () {
    var hotCurPage=parseInt($("#hotCurPage").text());
    if(hotCurPage!=1){
        $("#hotCurPage").text((hotCurPage-1));
        hotCurPage=parseInt($("#hotCurPage").text());
        writeContentList(hotCurPage,"main");
    }
});
$("#hotNextPage").click(function () {
    var hotCurPage=parseInt($("#hotCurPage").text());
    var hotTotPage=parseInt($("#hotTotPage").text());
    if(hotCurPage!=hotTotPage){
        $("#hotCurPage").text((hotCurPage+1));
        hotCurPage=parseInt($("#hotCurPage").text());
        writeContentList(hotCurPage,"main");
    }
});
$("#moviePageList").on("click","li:not(:first,:last)",function () {
    var currentPage=parseInt($(this).find('a').html());
    writePageList(currentPage,moviePageTotal,currentPage,"moviePageList");
    writeContentList(currentPage,"movie");
});
$("#moviePageList").on("click","li:eq(0)",function () {
    writePageList(1,moviePageTotal,1,"moviePageList");
    writeContentList(1,"movie");
});
$("#moviePageList").on("click","li:last",function () {
    writePageList(moviePageTotal,moviePageTotal,moviePageTotal,"moviePageList");
    writeContentList((moviePageTotal),"movie");
});

$("#comicPageList").on("click","li:not(:first,:last)",function () {
    var currentPage=parseInt($(this).find('a').html());
    writePageList(currentPage,comicPageTotal,currentPage,"comicPageList");
    writeContentList(currentPage,"comic");
});
$("#comicPageList").on("click","li:eq(0)",function () {
    writePageList(1,comicPageTotal,1,"comicPageList");
    writeContentList(1,"comic");
});
$("#comicPageList").on("click","li:last",function () {
    writePageList(comicPageTotal,comicPageTotal,comicPageTotal,"comicPageList");
    writeContentList((comicPageTotal),"comic");
});

$("#tvShowPageList").on("click","li:not(:first,:last)",function () {
    var currentPage=parseInt($(this).find('a').html());
    writePageList(currentPage,tvShowPageTotal,currentPage,"tvShowPageList");
    writeContentList(currentPage,"tvShow");
});
$("#tvShowPageList").on("click","li:eq(0)",function () {
    writePageList(1,tvShowPageTotal,1,"tvShowPageList");
    writeContentList(1,"tvShow");
});
$("#tvShowPageList").on("click","li:last",function () {
    writePageList(tvShowPageTotal,tvShowPageTotal,tvShowPageTotal,"tvShowPageList");
    writeContentList((tvShowPageTotal),"tvShow");
});
$("a[name='HomeMain']").click(function () {
    window.location.href="http://localhost:8085/bssp/main"
});
$("#article").on("click","[href='##']",function () {
    $("#detail").removeClass("hidden");
    $("#Home").addClass("hidden");
    $("#SortHome").addClass("hidden");
    var id=($(this).children("#id2").val());
    var code=$(this).children("#TypeId2").val();
    detail(code,id);
});
$(".container").on("click","[href='#']",function () {
    $("#detail").addClass("hidden");
    $("#Home").removeClass("hidden");
});
$("#searchbutton").click(function () {
    $("#detail").addClass("hidden");
    $("#Home").removeClass("hidden");
});
/*$('#mainLogin').click(function () {
    layer.msg("aa",1000,'');
});
$('#mainRegister').click(function () {
    layer.msg("aa",1000,'');
});*/
//--------------------------------------这是单击事件结束-------------------------------------------------//
function initPageTotal() {
    $.ajax({
        url: _server + "/bssp/main/page",
        type: 'post',
        dataType: 'json',
        data: {currentPage:'1'},
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function (result) {
            moviePageTotal=Math.ceil((result.data.moviePageTotal)/5);;
            comicPageTotal=Math.ceil((result.data.comicPageTotal)/5);
            tvShowPageTotal=Math.ceil((result.data.tvShowPageTotal)/5);
            $("#hotCurPage").text(1);
            hotTotPage=Math.ceil((result.data.hotTotPage)/5);
            $("#hotTotPage").text(hotTotPage);
            writePageList(1,moviePageTotal,1,"moviePageList");
            writePageList(1,comicPageTotal,1,"comicPageList");
            writePageList(1,tvShowPageTotal,1,"tvShowPageList");
        },
        error: function (result) {
            layer.msg("首页请求失败",{time:1500});
        }
    });
}
function detail(code,id) {
    $.ajax({
        url: _server + "/bssp/"+code+"/findById",
        type: 'post',
        dataType: 'json',
        data: {id:id},
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function (result) {
            console.log(result);
            $("#titleDetail h3 strong").html(result.name);
            $("#contentDetail").html(result.detail);
        },
        error: function (result) {
            layer.msg("首页请求失败",{time:1500});
        }
    });

}
function initContet() {
    $("#detail").addClass("hidden");
    $("#Home").removeClass("hidden");
    $.ajax({
        url: _server + "/bssp/type/list",
        type: 'post',
        dataType: 'json',
        data: {},
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function (result) {
            $.each(result.data, function(i, n) {
                $("#hotsort").append(
                "<a href='#' id="+n.typecd+" class='adi'>"+n.typename+"</a>"
                )});
        },
        error: function (result) {
            layer.msg("首页请求失败",{time:1500});
        }
    });
    $("#quitLogin").hide();
}
/**
 * 获取内容，并填充
 * @param currentPage
 * @param contentId
 * @author yongqiangyao
 */
function writeContentList(currentPage,contentId) {
    $.ajax({
        url: _server + "/bssp/"+contentId+"/list",
        type: 'post',
        dataType: 'json',
        data: {"currentPage":currentPage},
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function (result) {
            contentId=contentId+"ContentList";
            writeContent(result.data,contentId);
        },
        error: function (result) {
            layer.msg("首页请求失败",{time:1500});
        }
    });
}

//填充页面信息
function writePageList(curpage,wholePage,index,type){
    var pageContent = "";
    if (wholePage==0){
        pageContent += "暂无信息";
    }
    else {
        pageContent += "<li><a href='#'>&laquo;</a></li>";
        if (wholePage < 5) {
            for (var i = 0; i < wholePage; i++) {
                pageContent += "<li ><a href='#'>" + (i + 1) + "</a></li>";
            }
        }
        else {
            if (curpage == 1 || curpage == 2 || curpage == 3) {
                pageContent += "<li ><a href='#'>1</a></li>";
                pageContent += "<li><a href='#'>2</a></li>";
                pageContent += "<li ><a href='#'>3</a></li>";
                pageContent += "<li><a href='#'>4</a></li>";
                pageContent += "<li><a href='#'>5</a></li>";
            }
            else if (curpage == wholePage || curpage == (wholePage - 1) || curpage == (wholePage - 2)) {
                pageContent += "<li><a href='#'>" + (wholePage - 4) + "</a></li>";
                pageContent += "<li><a href='#'>" + (wholePage - 3) + "</a></li>";
                pageContent += "<li><a href='#'>" + (wholePage - 2) + "</a></li>";
                pageContent += "<li><a href='#'>" + (wholePage - 1) + "</a></li>";
                pageContent += "<li><a href='#'>" + wholePage + "</a></li>";
            }
            else {
                pageContent += "<li><a href='#'>" + (curpage - 2) + "</a></li>";
                pageContent += "<li><a href='#'>" + (curpage - 1) + "</a></li>";
                pageContent += "<li><a href='#'>" + (curpage) + "</a></li>";
                pageContent += "<li><a href='#'>" + (curpage + 1) + "</a></li>";
                pageContent += "<li><a href='#'>" + (curpage + 2) + "</a></li>";
            }
        }
        pageContent += "<li><a href='#'>&raquo;</a></li>";
    }
    $("#"+type).empty();
    $("#"+type).append(
        pageContent
    );
    if(curpage==wholePage){
        $("#"+type+" li a:contains('"+wholePage+"')").parent().addClass("active").siblings().removeClass("active");
    }
    $("#"+type+" li a:contains('"+index+"')").parent().addClass("active").siblings().removeClass("active");
}
//填充表格数据
function writeContent(data,contentId){
    $("#"+contentId).empty();
    //each  遍历arr对象
    $.each(data,function(arr, item) {
        var tdc = "<li><a class='play-img' href='##' title='"+item.name+"'>" +
            "<input type='text' class='hidden' id='id2' value='"+""+item.id+""+"' />"+
        "<input type='text' class='hidden' id='TypeId2' value='"+""+item.codeNm+""+"'/>"+
            "<img src='"+moviePicSr+"/"+item.imgSrc+"' alt='"+item.name+"'>" +
            "</a><b><a href='##' title='"+item.name+"' >"+""+item.name+""+"</a></b> <p>"+""+item.typeName+""+"</p> </li>";

        $("#"+contentId).append(tdc);
    });
}